﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using App2Wpf.ViewModels;
using System.Windows.Shapes;

namespace App2Wpf.Views
{
    /// <summary>
    /// plusRender.xaml 的交互逻辑
    /// </summary>
    public partial class ExBlitCard : UserControl, ICardUC
    {
        public int ID { get; set; }
        public nameip connectNameip { get; set; }

        public System.Net.IPAddress iPAddress;

        ViewerPlus view;

        WriteableBitmap wBitmap;
        public ExBlitCard(nameip serverinfo)
        {
            InitializeComponent();

            initLoading();

            connectNameip = serverinfo;

        }

        private void Close_Button_Click(object sender, RoutedEventArgs e)
        {
            view.session.CloseSession();
            ServiceLocator.rightWrapVM.removeCard(this);
        }

        public void initLoading()
        {
            LoadingG.Visibility = Visibility.Visible;
            MiniWindow.Visibility = Visibility.Hidden;
        }

        public void StartWork()
        {
            this.iPAddress = System.Net.IPAddress.Parse(connectNameip.ip);
            view = new ViewerPlus(iPAddress, connectNameip);

            view.OnConnect += () => {
                Dispatcher.Invoke(new Action(() =>
                {
                    LoadingG.Visibility = Visibility.Collapsed;
                    MiniWindow.Visibility = Visibility.Visible;
                }));
            };
            view.onRendering += ViewWindow_refreshCard2;

            Task.Run(()=> view.init(iPAddress)); //for loading not wait

            //wBitmap = new  WriteableBitmap(viewWindow.session.ViewerConfiguration.RemoteDesktopWidth,
            //                viewWindow.session.ViewerConfiguration.RemoteDesktopHeight, 96, 96, PixelFormats.Bgr24, null);
            wBitmap = new WriteableBitmap(1920,
                            1080, 96, 96, PixelFormats.Bgr32, null);
            MiniWindow.Source = wBitmap;
        }

        private void ViewWindow_refreshCard(byte[] tempBuffer, System.Windows.Int32Rect tempRect)
        {
            cardTitle.Text = view.VirtualDeskForm.cardTitle;

            // 调用线程无法访问此对象，因为另一个线程拥有该对象。  ==>  must use dispatcher...
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                this.wBitmap.Lock();
                System.Runtime.InteropServices.Marshal.Copy(tempBuffer, 0, this.wBitmap.BackBuffer, tempBuffer.Length);
                wBitmap.AddDirtyRect(tempRect);
                wBitmap.Unlock();
            }));
            
        }

        private void ViewWindow_refreshCard2(WriteableBitmap block, System.Windows.Int32Rect tempRect, byte[] buffer)
        {
            //无法直接使用 block 调用线程无法访问此对象，因为另一个线程拥有该对象。
            ////WriteableBitmap nblock = block.Clone();
            System.Windows.Point rectPoint = new System.Windows.Point(view.destPoint.X, view.destPoint.Y);
            
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                cardTitle.Text = view.VirtualDeskForm.cardTitle;
                // 调用线程无法访问此对象，因为另一个线程拥有该对象。
                WriteableBitmap nblock = new WriteableBitmap(clientFunc.BytesToBitmapImage(buffer));
                
                wBitmap.Lock(); //线程内对象才能锁住
                WriteableBitmapExtensions.Blit(wBitmap,
                    new System.Windows.Point(rectPoint.X, rectPoint.Y),    // 大图或者小图的目的坐标点
                    nblock,                                                  // 输入 writeablebitmap    
                    new System.Windows.Rect(0, 0, nblock.Width, nblock.Height), //// 输入 writeablebitmap 的 RECT，实际内部只取用高度和宽度
                    Colors.White,
                    WriteableBitmapExtensions.BlendMode.None);
                wBitmap.AddDirtyRect(tempRect); //在图像未锁定时无法调用此方法

                wBitmap.Unlock();
                MiniWindow.Source = wBitmap;
            }));
        }

    }
}
